package com.wt.util.jdbc;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

/**
 * 工具类
 */
public class Util {

    static String driver=null;
    static String url = null;
    static String user = null;
    static String password = null;
    static Connection con = null;

    static {
        Properties p = new Properties();
        try {
            InputStream is =Util.class.getClassLoader().getResourceAsStream("jdbc.properties");
            p.load(is);
            url = p.getProperty("url");
            user = p.getProperty("username");
            password = p.getProperty("password");
            driver = p.getProperty("driverClassName");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 通过Druid数据库连接池的方式来得到一个连接
     * @return
     */
    public static DataSource getCon(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(user);
        ds.setPassword(password);
        ds.setInitialSize(10);
        ds.setMaxActive(10);
        ds.setMaxWait(1000);
        return ds;
    }

}
